﻿using System.Xml.Linq;
using BattleFury.Lib.Extensions;
using BattleFury.Lib.Input;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Controls
{
	public class TextBox
		: Control
	{
		public static string PartBorderLeft = "ProgressBarLeft";
		public static string PartBorderCenter = "ProgressBarCenter";
		public static string PartBorderRight = "ProgressBarRight";

		private Part _leftBorderPart;
		private Part _centerBorderPart;
		private Part _rightBorderPart;

		private bool _hasFocus;
		private bool _cursorVisible;
		private double _elapsed;

		public string Text { get; set; }
		public Color BorderColor { get; set; }
		public Color ForegroundColor { get; set; }

		public override void Deserialize(XElement element, object callingObject)
		{
			base.Deserialize(element, callingObject);

			Text = element.GetString("Text");
			BorderColor = element.GetColor("BorderColor", Color.White);
			ForegroundColor = element.GetColor("ForegroundColor", Color.Black);
		}

		public override void Update(GameTime gameTime)
		{
			if (_leftBorderPart == null)
			{
				_leftBorderPart = Skin[PartBorderLeft];
				_centerBorderPart = Skin[PartBorderCenter];
				_rightBorderPart = Skin[PartBorderRight];
			}

			_elapsed += gameTime.ElapsedGameTime.TotalMilliseconds;

			var bounds = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
			var input = RenderEngine.Get().Input;

			if (input.Mouse.CurrentBounds.Intersects(bounds))
			{
				if (input.Mouse.IsMouseButtonPressed(MouseButtons.Left))
				{
					_hasFocus = true;
				}
			}
			if (!input.Mouse.CurrentBounds.Intersects(bounds))
			{
				if (input.Mouse.IsMouseButtonPressed(MouseButtons.Left))
				{
					_hasFocus = false;
				}
			}

			if (_elapsed > 500)
			{
				_cursorVisible = !_cursorVisible;
				_elapsed = 0;
			}

			if (_hasFocus)
			{
				Text = input.Keyboard.TextInput;
			}
		}

		public override void Draw(GameTime gameTime)
		{
			SpriteBatch.Draw(Texture, new Vector2(Position.X, Position.Y), _leftBorderPart.SourceBounds, BorderColor);

			var innerBorderWidth = (Size.X - (_leftBorderPart.Width + _rightBorderPart.Width));
			for (var i = 0; i < innerBorderWidth; i += _centerBorderPart.Width)
			{
				SpriteBatch.Draw(Texture, new Vector2(Position.X + _leftBorderPart.Width + i, Position.Y), _centerBorderPart.SourceBounds, BorderColor);
			}

			SpriteBatch.Draw(Texture, new Vector2(Position.X + innerBorderWidth + _centerBorderPart.Width, Position.Y), _rightBorderPart.SourceBounds, BorderColor);

			if (string.IsNullOrEmpty(Text)) Text = string.Empty;
			var textSize = Font.MeasureString(Text);
			var textPosition = new Vector2(Position.X, Position.Y); // + ((textSize.Y / 2)));
			var cursor = _hasFocus && _cursorVisible ? "|" : "";
			SpriteBatch.DrawString(Font, Text + cursor, textPosition + Vector2.One + new Vector2(4f, 0f), Color.Black.WithOpacity(0.5f));
			SpriteBatch.DrawString(Font, Text + cursor, textPosition + new Vector2(4f, 0f), ForegroundColor);
		}
	}
}